package com.screen.screendisplay.service.impl.electric;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.screen.screendisplay.entity.electric.ElectricSafetyenvPollutant;
import com.screen.screendisplay.mapper.electric.ElectricSafetyenvPollutantMapper;
import com.screen.screendisplay.service.electric.ElectricSafetyenvPollutantService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 火电安全环保污染物表 服务实现类
 * </p>
 *
 * @author xuesong
 * @since 2025-09-24
 */
@Service
public class ElectricSafetyenvPollutantServiceImpl extends ServiceImpl<ElectricSafetyenvPollutantMapper, ElectricSafetyenvPollutant> implements ElectricSafetyenvPollutantService {

    @Override
    public List<ElectricSafetyenvPollutant> getByYear(Integer year) {
        return baseMapper.selectByYear(year);
    }

    @Override
    public List<ElectricSafetyenvPollutant> getByYearAndMonth(Integer year, Integer month) {
        return baseMapper.selectByYearAndMonth(year, month);
    }

    @Override
    public ElectricSafetyenvPollutant getYearlyStatistics(Integer year) {
        return baseMapper.selectYearlyStatistics(year);
    }

    @Override
    public List<ElectricSafetyenvPollutant> getMonthlyTrend(Integer year) {
        QueryWrapper<ElectricSafetyenvPollutant> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("year", year);
        queryWrapper.orderBy(true, true, "month"); // 按月份升序排列
        return list(queryWrapper);
    }

    @Override
    public ElectricSafetyenvPollutant getYearlyReductionBenefit(Integer year) {
        // 查询年度总的减排数据
        ElectricSafetyenvPollutant statistics = getYearlyStatistics(year);
        
        if (statistics != null) {
            // 创建一个新的对象，只包含减排相关数据
            ElectricSafetyenvPollutant reductionBenefit = new ElectricSafetyenvPollutant();
            reductionBenefit.setYear(year);
            reductionBenefit.setCo2Reduction(statistics.getCo2Reduction() != null ? statistics.getCo2Reduction() : BigDecimal.ZERO);
            reductionBenefit.setCoalSaving(statistics.getCoalSaving() != null ? statistics.getCoalSaving() : BigDecimal.ZERO);
            
            // 计算减排率（这里可以根据实际业务需求调整计算方式）
            if (statistics.getCo2Emission() != null && statistics.getCo2Emission().compareTo(BigDecimal.ZERO) > 0) {
                // 计算减排率并可以通过扩展字段或其他方式返回
                // BigDecimal reductionRate = statistics.getCo2Reduction()
                //     .divide(statistics.getCo2Emission().add(statistics.getCo2Reduction()), 4, RoundingMode.HALF_UP)
                //     .multiply(new BigDecimal("100"));
            }
            
            return reductionBenefit;
        }
        
        return null;
    }
}
